#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "module.h"
#include <stdint.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <sys/select.h>
#include "keymgt.h"
#include <sys/err.h>
#include "apitrans.h"

extern sys_apitrans_t sysapi;

int sec_key_injection( void )
{
    int (*psec_key_injection)( void );
    
    psec_key_injection = sysapi(SEC_KEY_INJECTION_API);
    if (psec_key_injection) {
        return psec_key_injection();
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_crl_injection( void )
{
    int (*psec_crl_injection)( void );
    
    psec_crl_injection = sysapi(SEC_CRL_INJECTION_API);
    if (psec_crl_injection) {
        return psec_crl_injection();
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_get_keygrp_no( uint8_t *fid, uint8_t *gid )
{
    int (*psec_get_keygrp_no)( uint8_t *fid, uint8_t *gid );
    
    psec_get_keygrp_no = sysapi(SEC_GET_KEYGRP_NO_API);
    if (psec_get_keygrp_no) {
        return psec_get_keygrp_no(fid, gid);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_set_curr_keygrp( uint8_t fid, uint8_t gid )
{
    int (*psec_set_curr_keygrp)( uint8_t fid, uint8_t gid );
    
    psec_set_curr_keygrp = sysapi(SEC_SET_CURR_KEYGRP_API);
    if (psec_set_curr_keygrp) {
        return psec_set_curr_keygrp(fid, gid);
    }
    return -API_UNSUPPORTED_ERR;
}

int  sec_set_workkey( uint8_t fid, uint8_t gid, 
                            uint8_t type, sec_set_workkey_t *key_info )
{
    int  (*psec_set_workkey)( uint8_t fid, uint8_t gid, 
                                uint8_t type, sec_set_workkey_t *key_info );
    
    psec_set_workkey = sysapi(SEC_SET_WORKKEY_API);
    if (psec_set_workkey) {
        return psec_set_workkey(fid, gid, type, key_info);
    }
    return -API_UNSUPPORTED_ERR;
}
		
int sec_pin_encrypt( pin_block_type_t format, sec_pin_encrypt_t *info, 
					uint8_t * result)
{
    int (*psec_pin_encrypt)( pin_block_type_t format, sec_pin_encrypt_t *info, 
                        uint8_t * result);
    
    psec_pin_encrypt = sysapi(SEC_PIN_ENCRYPT_API);
    if (psec_pin_encrypt) {
        return psec_pin_encrypt(format, info, result);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_mac_9606(uint8_t* in, uint32_t len, uint8_t *out)
{
    int (*psec_mac_9606)(uint8_t* in, uint32_t len, uint8_t *out);
    
    psec_mac_9606 = sysapi(SEC_MAC_9690_API);
    if (psec_mac_9606) {
        return psec_mac_9606(in, len, out);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_mac_x99(uint8_t* in, uint32_t len, uint8_t *out)
{
    int (*psec_mac_x99)(uint8_t* in, uint32_t len, uint8_t *out);
    
    psec_mac_x99 = sysapi(SEC_MAC_X99_API);
    if (psec_mac_x99) {
        return psec_mac_x99(in, len, out);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_mac_x919(uint8_t* in, uint32_t len, uint8_t *out)
{
    int (*psec_mac_x919)(uint8_t* in, uint32_t len, uint8_t *out);
    
    psec_mac_x919 = sysapi(SEC_MAC_X919_API);
    if (psec_mac_x919) {
        return psec_mac_x919(in, len, out);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_track_encrypt( uint8_t *track_bcd, int len )
{
    int (*psec_track_encrypt)( uint8_t *track_bcd, int len );
    
    psec_track_encrypt = sysapi(SEC_TRACK_ENCRYPT_API);
    if (psec_track_encrypt) {
        return psec_track_encrypt(track_bcd, len);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_mac_dc_ecb(uint8_t* in, int len, uint8_t *out)
{
    int (*psec_mac_dc_ecb)(uint8_t* in, uint32_t len, uint8_t *out);
    
    psec_mac_dc_ecb = sysapi(SEC_MAC_DC_ECB_API);
    if (psec_mac_dc_ecb) {
        return psec_mac_dc_ecb(in, len, out);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_unset_key( uint8_t fid, uint8_t gid, uint8_t type )
{
    int (*psec_unset_key)( uint8_t fid, uint8_t gid, uint8_t type );
    
    psec_unset_key = sysapi(SEC_UNSET_KEY_API);
    if (psec_unset_key) {
        return psec_unset_key(fid, gid, type);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_set_masterkey( uint8_t gid, sec_set_masterkey_t *key_info )
{
    int (*psec_set_masterkey)( uint8_t gid, sec_set_masterkey_t *key_info );
    
    psec_set_masterkey = sysapi(SEC_SET_MASTERKEY_API);
    if (psec_set_masterkey) {
        return psec_set_masterkey(gid, key_info);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_update_masterkey( uint8_t gid, upt_masterkey_t *key_info )
{
    int (*psec_update_masterkey)( uint8_t gid, upt_masterkey_t *key_info );
    
    psec_update_masterkey = sysapi(SEC_UPDATE_MASTERKEY_API);
    if (psec_update_masterkey) {
        return psec_update_masterkey(gid, key_info);
    }
    return -API_UNSUPPORTED_ERR;
}



int sec_sm2_verify_signature( sm2_sign_t *sign, sm2_para_t *para )
{
    int (*psec_sm2_verify_signature)( sm2_sign_t *sign, sm2_para_t *para );
    
    psec_sm2_verify_signature = sysapi(SEC_SM2_VERIFY_SIGNATURE_API);
    if (psec_sm2_verify_signature) {
        return psec_sm2_verify_signature(sign, para);
    }
    return -API_UNSUPPORTED_ERR;
}

int sec_sm3_calc( uint8_t *in, int len, uint8_t *out )
{
    int (*psec_sm3_calc)( uint8_t *in, int len, uint8_t *out );
    
    psec_sm3_calc = sysapi(SEC_SM3_CALC_API);
    if (psec_sm3_calc) {
        return psec_sm3_calc(in, len, out);
    }
    return -API_UNSUPPORTED_ERR;
}
